package org.teachertools.web.controller;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.teachertools.data.Competence;
import org.teachertools.services.CategorieService;
import org.teachertools.services.CompetenceService;
import org.teachertools.services.MatiereService;
import org.teachertools.services.exception.ServiceException;
import org.teachertools.web.forms.CompetenceForm;
import org.teachertools.web.forms.CompetenceFormMapping;
import org.teachertools.web.validator.CompetenceValidator;

@Controller
@RequestMapping("/competences/*")
public class CompetenceController {

    private static final Logger logger = Logger.getLogger(CompetenceController.class);

    @Resource
    private CompetenceService competenceService;
    @Resource
    private CategorieService categorieService;
    @Resource
    private MatiereService matiereService;

    @RequestMapping("show")
    public String searchCompetences(ModelMap model, @ModelAttribute CompetenceForm competence) throws ServiceException {
        logger.debug("method searchCompetences called in the controller");
        List<Competence> competences = this.competenceService.searchByExample(CompetenceFormMapping
                .mappingFormToDTO(competence));
        if (logger.isDebugEnabled()) {
            logger.debug("Found " + competences.size() + " competences");
        }
        model.addAttribute("categories", this.categorieService.getList());
        model.addAttribute("matieres", this.matiereService.getList());
        model.addAttribute("competences", competences);
        return "competences/show";
    }

    @RequestMapping("ajax/show")
    public String ajaxSearchCompetences(ModelMap model, @ModelAttribute CompetenceForm competence)
            throws ServiceException {
        logger.debug("Ajax Invocation of searchCompetences");
        searchCompetences(model, competence);
        return "competences/results";
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String prepareAddCompetence(ModelMap model) throws ServiceException {
        logger.debug("method prepareAddCompetence called in the controller");
        model.addAttribute("competence", new CompetenceForm());
        return "competences/view";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String addCompetence(ModelMap model, @ModelAttribute CompetenceForm competence, BindingResult result)
            throws ServiceException {
        logger.debug("method addCompetence called in the controller");
        new CompetenceValidator().validate(competence, result);
        if (result.hasErrors()) {
            logger.debug("Validation has errors, no action");
            return "competences/view";
        } else {
            this.competenceService.save(CompetenceFormMapping.mappingFormToDTO(competence));
        }
        LinkedList<String> messages = new LinkedList<String>();
        messages.add("success.save");
        model.addAttribute("messages", messages);
        return searchCompetences(model, new CompetenceForm());
    }

    @RequestMapping(value = "view/{id}", method = RequestMethod.GET)
    public String viewCompetence(ModelMap model, @PathVariable("id") long competenceId) throws ServiceException {
        logger.debug("method viewCompetence called in the controller");
        model.addAttribute("competence", this.competenceService.get(competenceId));
        return "competences/view";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String updateCompetence(ModelMap model, @ModelAttribute CompetenceForm competence, BindingResult result)
            throws ServiceException {
        logger.debug("method updateCompetence called in the controller");
        new CompetenceValidator().validate(competence, result);
        Competence loadedCompetence = this.competenceService.get(competence.getId());
        loadedCompetence.setNom(competence.getNom());
        this.competenceService.update(loadedCompetence);

        LinkedList<String> messages = new LinkedList<String>();
        messages.add("success.save");
        model.addAttribute("messages", messages);
        return viewCompetence(model, competence.getId());
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String deleteCompetence(ModelMap model, @PathVariable("id") long competenceId) throws ServiceException {
        logger.debug("method deleteCompetence called in the controller");
        this.competenceService.deleteById(competenceId);
        return searchCompetences(model, new CompetenceForm());
    }
}
